package simulator.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.VerticalAlignment;

import simulator.Scenario;
import simulator.software.Process;

/**
 * Charting component that paints a pie chart depicting CPU utilization by
 * process.
 * 
 * @author Kyle Loomis
 * 
 */
public class UtilizationChart extends JComponent
{
   private static final long serialVersionUID = 1L;
   private Scenario scenario = null;
   private DefaultPieDataset dataset = null;
   private JFreeChart utilChart = null;

   /**
    * Default constructor.
    * 
    * @param scenario
    *           the scenario on which the chart should be based
    */
   public UtilizationChart(Scenario scenario)
   {
      this.scenario = scenario;

      dataset = new DefaultPieDataset();

      Font normal = new Font("Arial", Font.PLAIN, 12);

      Color[] processColors = new Color[10];
      processColors[0] = new Color(255, 205, 205);
      processColors[1] = new Color(205, 255, 205);
      processColors[2] = new Color(205, 205, 255);
      processColors[3] = new Color(255, 255, 205);
      processColors[4] = new Color(255, 205, 255);
      processColors[5] = new Color(205, 255, 255);
      processColors[6] = new Color(205, 205, 205);
      processColors[7] = new Color(255, 205, 170);
      processColors[8] = new Color(255, 170, 205);
      processColors[9] = new Color(170, 205, 255);

      PiePlot3D utilPlot = new PiePlot3D(dataset);
      utilPlot.setForegroundAlpha(0.5f);
      int index = 0;
      dataset.setValue("OS", 1.0);
      utilPlot.setSectionPaint("OS", new Color(255, 255, 255));

      for (Process p : scenario.getComputer().getKernel().getProcesses())
      {
         dataset.setValue(p.getName(), p.getCyclesRunning());

         if (index < processColors.length)
         {
            utilPlot.setSectionPaint(p.getName(), processColors[index]);
         }

         index++;
      }

      utilChart = new JFreeChart(utilPlot);
      utilChart.setBackgroundPaint(Color.white);
      utilChart.setAntiAlias(true);
      utilChart.setBorderVisible(false);
      utilChart.setPadding(new RectangleInsets(0.0, 0.0, 0.0, 0.0));
      utilChart
            .setTitle(new TextTitle("CPU Utilization", normal, new Color(100,
                  100, 100), RectangleEdge.TOP, HorizontalAlignment.LEFT,
                  VerticalAlignment.BOTTOM, new RectangleInsets(0.0, 10.0, 0.0,
                        0.0)));

      utilChart.removeLegend();
   }

   public Dimension getMinimumSize()
   {
      return new Dimension(350, 200);
   }

   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);

      Graphics2D area = (Graphics2D) g;

      dataset.setValue("OS", scenario.getComputer().getKernel()
            .getCyclesRunning());

      for (Process p : scenario.getComputer().getKernel().getProcesses())
      {
         dataset.setValue(p.getName(), p.getCyclesRunning());
      }

      utilChart.draw(area, area.getClipBounds());
   }

}
